Conditions | 14 |
Total Lines | 61 |
Code Lines | 43 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like utf8-parser.js ➔ unpack often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* |
||
43 | export function unpack(buffer, start=0, end=buffer.length) { |
||
44 | /** @type {string} */ |
||
45 | let str = ''; |
||
46 | for(let index = start; index < end;) { |
||
47 | /** @type {number} */ |
||
48 | let lowerBoundary = 0x80; |
||
49 | /** @type {number} */ |
||
50 | let upperBoundary = 0xBF; |
||
51 | /** @type {boolean} */ |
||
52 | let replace = false; |
||
53 | /** @type {number} */ |
||
54 | let charCode = buffer[index++]; |
||
55 | if (charCode >= 0x00 && charCode <= 0x7F) { |
||
56 | str += String.fromCharCode(charCode); |
||
57 | } else { |
||
58 | /** @type {number} */ |
||
59 | let count = 0; |
||
60 | if (charCode >= 0xC2 && charCode <= 0xDF) { |
||
61 | count = 1; |
||
62 | } else if (charCode >= 0xE0 && charCode <= 0xEF ) { |
||
63 | count = 2; |
||
64 | if (buffer[index] === 0xE0) { |
||
65 | lowerBoundary = 0xA0; |
||
66 | } |
||
67 | if (buffer[index] === 0xED) { |
||
68 | upperBoundary = 0x9F; |
||
69 | } |
||
70 | } else if (charCode >= 0xF0 && charCode <= 0xF4 ) { |
||
71 | count = 3; |
||
72 | if (buffer[index] === 0xF0) { |
||
73 | lowerBoundary = 0x90; |
||
74 | } |
||
75 | if (buffer[index] === 0xF4) { |
||
76 | upperBoundary = 0x8F; |
||
77 | } |
||
78 | } else { |
||
79 | replace = true; |
||
80 | } |
||
81 | charCode = charCode & (1 << (8 - count - 1)) - 1; |
||
82 | for (let i = 0; i < count; i++) { |
||
83 | if (buffer[index] < lowerBoundary || buffer[index] > upperBoundary) { |
||
84 | replace = true; |
||
85 | } |
||
86 | charCode = (charCode << 6) | (buffer[index] & 0x3f); |
||
87 | index++; |
||
88 | } |
||
89 | if (replace) { |
||
90 | str += String.fromCharCode(0xFFFD); |
||
91 | } |
||
92 | else if (charCode <= 0xffff) { |
||
93 | str += String.fromCharCode(charCode); |
||
94 | } else { |
||
95 | charCode -= 0x10000; |
||
96 | str += String.fromCharCode( |
||
97 | ((charCode >> 10) & 0x3ff) + 0xd800, |
||
98 | (charCode & 0x3ff) + 0xdc00); |
||
99 | } |
||
100 | } |
||
101 | } |
||
102 | return str; |
||
103 | } |
||
104 | |||
156 |